1   /*
2    * Copyright (C) 2013 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import static com.google.common.collect.CollectPreconditions.checkEntryNotNull;
20  
21  import com.google.common.annotations.GwtIncompatible;
22  
23  import javax.annotation.Nullable;
24  
25  /**
26   * Implementation of {@code Map.Entry} for {@link ImmutableMap} that adds extra methods to traverse
27   * hash buckets for the key and the value. This allows reuse in {@link RegularImmutableMap} and
28   * {@link RegularImmutableBiMap}, which don't have to recopy the entries created by their
29   * {@code Builder} implementations.
30   *
31   * @author Louis Wasserman
32   */
33  @GwtIncompatible("unnecessary")
34  abstract class ImmutableMapEntry<K, V> extends ImmutableEntry<K, V> {
35    ImmutableMapEntry(K key, V value) {
36      super(key, value);
37      checkEntryNotNull(key, value);
38    }
39  
40    ImmutableMapEntry(ImmutableMapEntry<K, V> contents) {
41      super(contents.getKey(), contents.getValue());
42      // null check would be redundant
43    }
44  
45    @Nullable
46    abstract ImmutableMapEntry<K, V> getNextInKeyBucket();
47  
48    @Nullable
49    abstract ImmutableMapEntry<K, V> getNextInValueBucket();
50  
51    static final class TerminalEntry<K, V> extends ImmutableMapEntry<K, V> {
52      TerminalEntry(ImmutableMapEntry<K, V> contents) {
53        super(contents);
54      }
55  
56      TerminalEntry(K key, V value) {
57        super(key, value);
58      }
59  
60      @Override
61      @Nullable
62      ImmutableMapEntry<K, V> getNextInKeyBucket() {
63        return null;
64      }
65  
66      @Override
67      @Nullable
68      ImmutableMapEntry<K, V> getNextInValueBucket() {
69        return null;
70      }
71    }
72  }